/* eslint-disable consistent-return */
import { message } from 'antd';
import {
  getDictCode,
  getEmpList,
  queryDictItemsByCodes,
  addInsert,
  queryNmAreaByParent,
  updateGet,
  update,
  bankGetList,
  queryNmArea,
  deleteMul,
  bankGet,
  bankInsert,
  bankUpdate,
  contactGetList,
  contactDelete,
  contactGet,
  contactInsert,
  contactUpdate,
  getCostCenter,
  delCostCenter,
  costcenterInsert,
  getvatCoprName,
  getInvoiceList,
  invoiceDeleteMul,
  invoiceGet,
  invoiceInsert,
  invoiceUpdate,
  mailingaddressGetList,
  mailingaddressDeleteMul,
  mailingaddressGet,
  mailingaddressInsert,
  mailingaddressUpdate,
  supplyinvoiceGet,
  suppliyinvoiceUpdate,
  getSuppInfo,
  fetchCount,
  idxDeleteMul,
  updateStatus,
  dataRecord,
} from '@/services/supplier';

export default {
  state: {
    activeKey: '1',
    formFieldsValue: {},
    expandForm: false,
    supplierCode: [], // 供应商名称
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    tabList: [],
    invoiceTypeList: [],
    taxerTypeList: [],
    bankList: [],
    genderList: [],
    hasChildList: [],
    areaList: [],
    costCenterList: [],
    taxerNameList: [],
    updateTabList: [],
  },
  effects: {
    *dataRecord({ payload }, { call }) {
      const res = yield call(dataRecord, payload);
      return res;
    },
    *updateStatus({ payload }, { call }) {
      const res = yield call(updateStatus, payload);
      return res;
    },
    *idxDeleteMul({ payload }, { call }) {
      const res = yield call(idxDeleteMul, payload);
      return res;
    },
    *fetchCount({ payload }, { call }) {
      const res = yield call(fetchCount, payload);
      return res;
    },
    *getSuppInfo({ payload }, { call }) {
      const res = yield call(getSuppInfo, payload);
      return res;
    },
    *suppliyinvoiceUpdate({ payload }, { call }) {
      const res = yield call(suppliyinvoiceUpdate, payload);
      return res;
    },
    *supplyinvoiceGet({ payload }, { call }) {
      const res = yield call(supplyinvoiceGet, payload);
      return res;
    },
    *mailingaddressUpdate({ payload }, { call }) {
      const res = yield call(mailingaddressUpdate, payload);
      return res;
    },
    *mailingaddressInsert({ payload }, { call }) {
      const res = yield call(mailingaddressInsert, payload);
      return res;
    },
    *mailingaddressGet({ payload }, { call }) {
      const res = yield call(mailingaddressGet, payload);
      return res;
    },
    *mailingaddressDeleteMul({ payload }, { call }) {
      const res = yield call(mailingaddressDeleteMul, payload);
      return res;
    },
    *mailingaddressGetList({ payload }, { call }) {
      const res = yield call(mailingaddressGetList, payload);
      return res;
    },
    *invoiceUpdate({ payload }, { call }) {
      const res = yield call(invoiceUpdate, payload);
      return res;
    },
    *invoiceInsert({ payload }, { call }) {
      const res = yield call(invoiceInsert, payload);
      return res;
    },
    *invoiceGet({ payload }, { call }) {
      const res = yield call(invoiceGet, payload);
      return res;
    },
    *invoiceDeleteMul({ payload }, { call }) {
      const res = yield call(invoiceDeleteMul, payload);
      return res;
    },
    *getInvoiceList({ payload }, { call }) {
      const res = yield call(getInvoiceList, payload);
      return res;
    },
    *getvatCoprName({ payload }, { call, put }) {
      const res = yield call(getvatCoprName, payload);
      if (res.success) {
        yield put({
          type: 'updateModalData',
          payload: {
            taxerNameList: res.data,
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    *costcenterInsert({ payload }, { call }) {
      const res = yield call(costcenterInsert, payload);
      if (res.success) {
        message.success('操作成功');
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    *delCostCenter({ payload }, { call }) {
      const res = yield call(delCostCenter, payload);
      if (res.success) {
        message.success('操作成功');
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    *getCostCenter({ payload }, { call, put }) {
      const res = yield call(getCostCenter, payload);
      if (res.success) {
        yield put({
          type: 'updateModalData',
          payload: {
            costCenterList: res.data,
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    *contactUpdate({ payload }, { call }) {
      const res = yield call(contactUpdate, payload);
      return res;
    },
    *contactInsert({ payload }, { call }) {
      const res = yield call(contactInsert, payload);
      return res;
    },
    *contactGet({ payload }, { call }) {
      const res = yield call(contactGet, payload);
      return res;
    },
    *contactDelete({ payload }, { call }) {
      const res = yield call(contactDelete, payload);
      return res;
    },
    *contactGetList({ payload }, { call }) {
      const res = yield call(contactGetList, payload);
      return res;
    },
    *bankUpdate({ payload }, { call }) {
      const res = yield call(bankUpdate, payload);
      return res;
    },
    *bankInsert({ payload }, { call }) {
      const res = yield call(bankInsert, payload);
      return res;
    },
    *bankGet({ payload }, { call }) {
      const res = yield call(bankGet, payload);
      return res;
    },
    *deleteMul({ payload }, { call }) {
      const res = yield call(deleteMul, payload);
      return res;
    },
    *queryNmArea({ payload }, { call, put }) {
      const res = yield call(queryNmArea, payload);
      if (res.success) {
        yield put({
          type: 'updateModalData',
          payload: {
            areaList: res.data,
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    *bankGetList({ payload }, { call }) {
      const res = yield call(bankGetList, payload);
      return res;
    },
    *update({ payload }, { call }) {
      const res = yield call(update, payload);
      return res;
    },
    *updateGet({ payload }, { call }) {
      const res = yield call(updateGet, payload);
      return res;
    },
    *queryNmAreaByParent({ payload }, { call }) {
      const res = yield call(queryNmAreaByParent, payload);
      return res;
    },
    *addInsert({ payload }, { call }) {
      const res = yield call(addInsert, payload);
      return res;
    },
    *queryDictItemsByCodes({ payload }, { call, put }) {
      const res = yield call(queryDictItemsByCodes, payload);
      if (res.success) {
        const sortNumber = (a, b) => a.itemCode - b.itemCode;
        if (res.data.CUST_TAB) {
          res.data.CUST_TAB.sort(sortNumber);
        }
        if (res.data.SUPPER_DETAIL_TAB) {
          res.data.SUPPER_DETAIL_TAB.sort(sortNumber);
        }

        yield put({
          type: 'updateModalData',
          payload: {
            updateTabList: res.data.SUPPER_DETAIL_TAB || [],
            tabList: res.data.CUST_TAB || [],
            invoiceTypeList: res.data.MINI_INV_TYPE || [],
            taxerTypeList: res.data.MINI_INV_TYPE || [],
            bankList: res.data.BANK_ID || [],
            genderList: res.data.GENDER || [],
            hasChildList: res.data.HAS_CHILD || [],
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
      return res;
    },
    // 获取供应商名称字典
    *fetchSupplyCode({ payload, callback }, { call, put }) {
      let response = null;
      response = yield call(getDictCode, payload);
      if (response.success) {
        yield put({
          type: 'getDictCode',
          payload: Array.isArray(response.data) ? response.data : [],
        });
        if (callback && typeof callback === 'function') {
          callback(response.data);
        }
        return response.data;
      }
    },
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getEmpList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
  },

  reducers: {
    getDictCode(state, { payload }) {
      return {
        ...state,
        supplierCode: payload,
      };
    },
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 设置修改当前state数据
    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 设置查询时保存的值
    updateTabListQuery(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
